from selenium.webdriver.common.by import By
import time
from utils.base_aolai import BaseObject, BaseHandle
import logging

class LoginObject(BaseObject):
    def __init__(self):
        super().__init__()
        self.name = (By.ID, 'com.yunmall.lc:id/logon_account_textview')
        self.pwd = (By.ID, 'com.yunmall.lc:id/logon_password_textview')
        self.login_btu = (By.ID, 'com.yunmall.lc:id/logon_button')
        self.close_btu=(By.ID,'com.yunmall.lc:id/ymtitlebar_left_btn_image')

    def findName(self):
        return self.search_ele(self.name)

    def findPwd(self):
        return self.search_ele(self.pwd)

    def findLogin_btu(self):
        return self.search_ele(self.login_btu)
    def find_login_close(self):
        return self.search_ele(self.close_btu)


class LoginHandle(BaseHandle):
    def __init__(self):
        super().__init__()
        self.lo = LoginObject()

    def name_in(self, text):
        logging.info("输入用户名:{}".format(text))
        self.input(self.lo.findName(), text)

    def pwd_in(self, text):
        logging.info("输入密码:{}".format(text))
        self.input(self.lo.findPwd(), text)

    def btu_tap(self):
        logging.info("点击登录按钮")
        self.lo.findLogin_btu().click()
    def click_close_btu(self):
        logging.info("点击登陆失败后的返回按钮")
        self.lo.find_login_close().click()


class LoginTask:
    logging.info("登录")
    lh = LoginHandle()

    @classmethod
    def input(cls, username, password):
        cls.lh.name_in(username)
        cls.lh.pwd_in(password)
        time.sleep(1)
        cls.lh.btu_tap()
    @classmethod
    def close(cls):
        cls.lh.click_close_btu()
